<template>
  <el-container>
    <el-container>
      <el-aside width="200px">
        <div class="xitong">leave managerment</div>
        <el-menu
          default-active="1-1"
          class="el-menu-vertical-demo"
          @open="handleOpen"
          @close="handleClose"
        >
          <el-submenu index="1">
            <template slot="title">
              <i class="el-icon-date"></i>
              <span slot="title">Application</span>
            </template>
            <el-menu-item-group>
              <el-menu-item index="1-1" @click="go_askforleave">ask for leave</el-menu-item>
            </el-menu-item-group>
            <el-menu-item-group>
              <el-menu-item index="1-2" @click="go_leaveManagerment">leave managerment</el-menu-item>
            </el-menu-item-group>
          </el-submenu>
          <el-submenu index="2">
            <template slot="title">
              <i class="el-icon-menu"></i>
              <span slot="title">Role</span>
            </template>
            <el-menu-item-group>
              <el-menu-item index="2-1" @click="go_role">role managerment</el-menu-item>
            </el-menu-item-group>
          </el-submenu>
          
        </el-menu>
      </el-aside>

      <el-main>
        <el-header>
          <el-menu
            :default-active="activeIndex"
            class="el-menu-demo"
            mode="horizontal"
            @select="handleSelect"
            background-color="#545c64"
            text-color="#fff"
            active-text-color="#ffd04b"
          >
          </el-menu>
          <div @click="exit">
            <el-avatar
              src="https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png"
            ></el-avatar>
          </div>
          <p class="timedate">Current time：{{ nowTime }}</p>
        </el-header>

        <div class="echarts">
        <div class="partyAdd">
    <div class="title">Ask for Leave</div>
    <div class="add-merchants-list">
      <el-form
        :model="params"
        :rules="rules"
        ref="ruleForm"
        label-width="170px"
        class="form-list"
      >
        <div class="form-item">
          <el-form-item label="Leave reason：" prop="reason">
            <el-input v-model="params.reason" placeholder="please fill in leave reason:"></el-input>
          </el-form-item>
        </div>
        <div class="form-item">
          <el-form-item label="Leave date：" prop="leavedate">
                        <el-date-picker
                                v-model="params.leavedate"
                                type="daterange"
                                format="yyyy-MM-dd"
                                value-format="yyyy-MM-dd"
                                range-separator="to"
                                start-placeholder="begin date"
                                end-placeholder="end date">
                        </el-date-picker>
          </el-form-item>
        </div>
        <div style="margin-left:60px;">
          <div style="line-height: 40px;"><i class="el-icon-warning-outline">Caution</i></div>
          <div style="line-height: 25px;">
          <span> a) If leave days > 10, need to senior manager approve it.</span>
        </div>
        <div style="line-height: 25px;">
          <span> b) If leave days < 10, need to manager approve it. After manager approval, then senior manager could double approve the request.</span>
        </div>
        </div>
        

      </el-form>
      <div class="btn-list">
        <el-button type="" onclick="window.history.go(-1)">Back</el-button>
        <el-button type="primary" :loading="BTNloading" @click="submitBtn('ruleForm')">{{
          BTNloading ? "loading" : "Save"
        }}</el-button>
      </div>
    </div>
  </div>
</div>
      </el-main>
    </el-container>
  </el-container>
  
</template>

<script>
import {
  goAddLeave, 
  loginOut, 
} from "../axios/api";
export default {
  components: {
    
  },
  data() {
    return {
      params: {
        reason: "",
        leavedate: "", 
      },
      activeIndex:"1",
      nowTime:"",
      isEmpty: false,
      BTNloading: false,
      rules: {
        reason: [{ required: true, message: "Please fill in leave reason.", trigger: "blur" }],
        leavedate: [{ required: true, message: "Please select leave date.", trigger: "blur" }],
      },
    };
  },
  mounted() {
  },
  created() {
    this.getTime();
  },
  methods: {
    handleSelect(key, keyPath) {
      console.log(key, keyPath);
    },
    handleOpen(key, keyPath) {
      console.log(key, keyPath);
    },
    handleClose(key, keyPath) {
      console.log(key, keyPath);
    },
    go_askforleave(){
      this.$router.push("askforleave");
    },
    go_role() {
      this.$router.push("role");
    },
    go_leaveManagerment(){
      this.$router.push("index");
    },
    
    getTime() {
      setInterval(() => {
        this.nowTime = new Date().toLocaleString();
      }, 1000);
    },

    exit() {
      loginOut().then((res) => {
        console.log(res);
        if ((res.stat = 1)) {
          this.$router.push("/");
        }
      });
    },

    // save
    submitBtn(formName) {
      console.log(this.params);
      this.$refs[formName].validate((valid) => {
        if (valid) {
            this.addLeave();
        } else {
          this.$message.warning("Please check the infomation is vaild");
          return false;
        }
      });
    },
    addLeave() {
      console.log("this.params.leavedate:"+this.params.leavedate)
      let params = {
        reason: this.params.reason, 
        begin_date: this.params.leavedate[0], 
        end_date: this.params.leavedate[1]

      };
      this.BTNloading = true;
      goAddLeave(params).then((res) => {
        console.log(res);
        if (res.stat == 1) {
          // this.added = true
          this.$message.success("Add Success");
          this.BTNloading = false;
          this.$router.go(-1); 
        } else {
          this.BTNloading = false;
          this.$message.error(res.msg);
        }
      });
    },
  },
};
</script>

<style lang="scss">
.echarts {
  width: 100%;
  height: 90vh;
}
.partyAdd {
  .title {
    padding: 0 20px;
    margin-left: 20px;
    font-size: 22px;
    position: relative;
    margin-bottom: 20px;
  }
  .title-mb20 {
    margin-bottom: 20px;
  }
  .title-mt20 {
    margin-top: 20px;
  }
  .title:after {
    content: "";
    width: 4px;
    height: 20px;
    background-color: #1890ff;
    position: absolute;
    left: 0;
    top: 50%;
    z-index: 1;
    transform: translate(-50%, -50%);
  }
  .dynamic-tags {
    .el-input {
      width: 150px;
    }
    .el-tag {
      margin-right: 10px;
    }
  }
  .form-item {
    width: 700px;
    .Suggestsize {
      font-size: 14px;
      color: gray;
    }
    .el-form-item {
      .el-form-item__content {
        .el-textarea {
          .el-textarea__inner {
            min-height: 100px !important;
          }
        }
      }
    }
  }
  .add-merchants-list {
    overflow: auto;
    margin-bottom: 20px;
  }
  .unitType-box {
    /*.unitType-*/
    display: flex;
    flex-wrap: wrap;
    .el-form-item {
      width: 50%;
    }
  }
  .unitType-add {
    .el-form-item__content {
      /*display: flex;*/
    }
  }
  .unitType-order {
    width: 50%;
  }
  .el-dialog__body {
    padding: 30px 20px 0;
  }
  .el-dialog__footer {
    padding-left: 120px;
    text-align: left;
  }
  .unitType-list {
    padding: 0 30px;
    display: flex;
    flex-wrap: wrap;
    .unitType-item {
      margin-bottom: 30px;
      margin-right: 30px;
      border-radius: 8px;
      position: relative;
      border: 1px solid #e4e4e4;
      width: 250px;
      height: 400px;
      .unitType-top {
        padding: 15px 20px;
      }
      .unitType-time,
      .unitType-info {
        font-size: 14px;
        color: #666666;
        margin: 10px 0;
      }
      .unitType-info {
        p {
          margin-bottom: 10px;
          overflow: hidden;
          white-space: nowrap;
          text-overflow: ellipsis;
        }
      }
      .unitType-img {
        width: 100%;
        height: 200px;
        .el-image {
          width: 100%;
          height: 100%;
        }
        img {
          width: 100%;
          height: 100%;
        }
      }
      .unitType-title {
        font-size: 24px;
        font-weight: 500;
        overflow: hidden;
        white-space: nowrap;
        text-overflow: ellipsis;
      }
      .unitType-item-add {
        cursor: pointer;
        width: 100%;
        height: 100%;
        display: flex;
        align-items: center;
        justify-content: center;
        .icon-jia {
          font-size: 50px;
          color: #666666;
        }
      }
      .unitType-btn {
        cursor: pointer;
        border-top: 1px solid #e4e4e4;
        position: absolute;
        bottom: 0;
        left: 0;
        width: 100%;
        height: 40px;
        display: flex;
        .unitType-btn-item {
          width: 50%;
          display: flex;
          justify-content: center;
          align-items: center;
          font-size: 14px;
          color: #666666;
          .iconfont {
            font-size: 14px;
            margin-right: 5px;
          }
        }
        .unitType-btn-item:first-child {
          border-right: 1px solid #e4e4e4;
        }
      }
    }
  }
  .btn-list {
    margin: 20px 170px;
  }
  .note-info {
    font-size: 13px;
    color: #606266;
    padding: 0 20px 20px 20px;
  }
}
.el-container {
  position: absolute;
  width: 100%;
  top: 0px;
  left: 0;
  bottom: 0;
}
.el-header {
  background-color: #b3c0d1;
  line-height: 60px;
  padding: 0;
  z-index: 1000;
}
.el-header .fr {
  float: right;
}

.el-header .el-menu {
  border-bottom: none;
}
.el-avatar {
  text-align: center;
  float: right;
  margin: 0.6rem 2rem 0.3rem 0;
}
.timedate {
  text-align: center;
  float: right;
  margin: 0 3rem 0.3rem 0;
}
.el-aside .el-menu {
  border-right: none;
}
.el-aside {
  background-color: white;
  line-height: 200px;
}
.xitong {
  height: 60px;
  text-align: center;
  line-height: 3;
  font-size: 20px;
  font-weight: bold;
}
.el-main {
  background-color: #fff;
  line-height: 160px;
  padding: 0;
}
.el-menu-vertical-demo:not(.el-menu--collapse) {
  width: 200px;
  min-height: 400px;
}
.echarts {
  width: 100%;
  height: 90vh;
}
.box {
  width: 98%;
  height: 43vh;
  margin-top: 20px;
  background-color: white;
  border-radius: 5px;
  margin-left: 20px;
  display: flex;
}
.el-table__header tr,
.el-table__header th {
  padding: 0;
  height: 80px;
  line-height: 80px;
}
.el-table__body tr,
.el-table__body td {
  padding: 0;
  height: 54.5px;
  line-height: 54.5px;
}
.el-menu-item-group__title{
  padding: 0px;
}
</style>
